package cn.zhangxin.newyear.system.department.controller;

import cn.zhangxin.newyear.system.department.service.IDepartmentService;
import cn.zhangxin.newyear.system.department.domain.Department;
import cn.zhangxin.newyear.system.department.query.DepartmentQuery;
import cn.zhangxin.newyear.utils.AjaxResult;
import cn.zhangxin.newyear.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    public IDepartmentService departmentService;


    /**
     * 保存和修改公用的
     * @param department  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存部门" )
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if( department.getId()!=null)
                departmentService.update(department);
            else
                departmentService.add(department);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "保存删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个对象" )
    public Department get(@PathVariable("id")Long id)
    {
        return departmentService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @ApiOperation(value = "获取全部对象" )
    public List<Department> list(){

        return departmentService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pagelist")
    @ApiOperation(value = "高级查询")
    public AjaxResult json(@RequestBody DepartmentQuery query)
    {
        try {
            List<Department> byQuery = departmentService.findByQuery(query);
            Long total = departmentService.findByQueryCount(query);
            System.out.println(total);
            return AjaxResult.success().setData(byQuery).setTotal(total);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    /**
    查询上级部门
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获得部门级树")
    public List<Department> getTree(){
        return departmentService.getTree();
    }
    /**
     *导出Excel
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Department> all = departmentService.findAll();
            ExcelUtil.exportExcel(all,null,"部门",Department.class,"dept.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 导入Excel
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        try {
            List<Department> depts = ExcelUtil.importExcel(file,1,1,Department.class);
            departmentService.batchAdd(depts);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
